//
//  ChatTableViewCell.m
//  EaseMobDemo
//
//  Created by yutao on 16/2/27.
//  Copyright © 2016年 yutao. All rights reserved.
//

#import "MyChatTableViewCell.h"

#define LABELWIDTH  160

@implementation MyChatTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

       [self createSubviews];
    }

    return self;
}

-(void)createSubviews
{
    self.contentView.backgroundColor = [UIColor yellowColor];

    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 60, 80)];
    self.bgImageView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.bgImageView];


    self.contentsLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, LABELWIDTH, 160)];
    [self.contentView addSubview:self.contentsLabel];
    //设置无限行数
    self.contentsLabel.numberOfLines = 0;
    //换行模式:按文字换行
    self.contentsLabel.lineBreakMode = NSLineBreakByWordWrapping;

}

-(void)setMessage:(EMMessage *)message
{
    _message = message;

        id body = [message.messageBodies lastObject];

        EMTextMessageBody *textBody = body;
        self.contentsLabel.text = textBody.text;
        NSLog(@"111====%@",textBody.text);

        CGFloat height = [[self class] heightForCell:textBody.text];

        CGRect tempRect = self.contentsLabel.frame;
        tempRect.size.height = height;
        self.contentsLabel.frame = tempRect;
        [self.contentsLabel sizeToFit];


        //添加约束
        [self.contentsLabel mas_makeConstraints:^(MASConstraintMaker *make) {

            make.right.equalTo(self).mas_equalTo(-20);
            make.top.equalTo(self).mas_equalTo(15);
//            make.width.mas_equalTo(self.contentsLabel.frame.size.width);
//            make.height.mas_equalTo(self.contentsLabel.frame.size.height);

        }];
        //限制label最宽的宽度是多少
        self.contentsLabel.preferredMaxLayoutWidth = LABELWIDTH;



        UIImage *image = [UIImage imageNamed:@"chat_sender_bg"];
        image = [image resizableImageWithCapInsets:(UIEdgeInsetsMake(image.size.height * 0.6, image.size.width * 0.4, image.size.height * 0.3, image.size.width * 0.4))];
        self.bgImageView.image = image;

        //添加约束
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.edges.equalTo(self.contentsLabel).mas_equalTo(UIEdgeInsetsMake(-5 ,-15, -5, -15));

        }];

}


+(CGFloat)heightForCell:(NSString *)content
{
    //根据内容算内容高度
    //参数1.指定内容宽度来计算内容高度
    //参数2.指定计算高度的方式
    //参数3.指定内容文字大小来计算内容高度
    //参数4.苹果备用参数,设置为nil
    CGSize size = CGSizeMake(LABELWIDTH, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];

    CGRect rect = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    return rect.size.height;

}



-(void)setContents:(NSString *)contents
{

    self.contentsLabel.text = contents;

    //label高度自适应
    CGFloat height = [[self class] heightForCell:contents];
    CGRect tempRect = self.contentsLabel.frame;
    tempRect.size.height = height;
    self.contentsLabel.frame = tempRect;
    [self.contentsLabel sizeToFit];


    UIImage *image = [UIImage imageNamed:@"chat_sender_bg"];
    image = [image resizableImageWithCapInsets:(UIEdgeInsetsMake(image.size.height * 0.6, image.size.width * 0.4, image.size.height * 0.3, image.size.width * 0.4))];
    self.bgImageView.image = image;

    //添加约束
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.contentsLabel).mas_equalTo(UIEdgeInsetsMake(-5 ,-15, -5, -15));

    }];

}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
